﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq.Mapping;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{

    [Export(typeof(ICart))]
    public partial class Cart : Entity, ICart
    {

        #region ICart Members

        ICustomer _customer;
        ICustomer ICart.Customer
        {
            get
            {
                if (this._customer == null)
                {
                    this._customer = this.DataContext.Customers
                        .Where(i => i.Id == this.CustomerId)
                        .FirstOrDefault();
                }

                return this._customer;
            }
            set
            {
                this._customer = value;
                if (value != null)
                    this.CustomerId = value.Id;
            }
        }
        IShipping _shipping;
        public IShipping Shipping
        {
            get
            {
                if (this._shipping == null)
                {
                    this._shipping = this.DataContext.Shippings
                        .Where(i => i.Id == this.ShippingId)
                        .FirstOrDefault();
                }

                return this._shipping;

            }
            set
            {
                this._shipping = value;
                this.ShippingId = value.Id;
            }
        }

        IPayment _payment;
        public IPayment Payment
        {
            get
            {
                if (this._payment == null)
                {
                    this._payment = this.DataContext.Payments
                        .Where(i => i.MethodName == this.PaymentName)
                        .FirstOrDefault();
                }

                return this._payment;
            }
            set
            {
                this._payment = value;
                this.PaymentName = value.MethodName;
            }
        }


        List<ICartItem> _cartItems;
        List<ICartItem> ICart.CartItems
        {
            get
            {
                if (this._cartItems == null)
                {
                    this._cartItems = this.DataContext.CartItems
                        .Where(i => i.CartId == this.Id)
                        .Select(i => i as ICartItem)
                        .ToList();
                }

                return this._cartItems;
            }
            set
            {
                this._cartItems = value;
            }
        }



        #endregion
    }
}
